#include <iostream>
using namespace std;
#include <map>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <functional>
#include <numeric>
#include <deque>

class Speaker{
        public:
                string name;
                int score[3];
};

//产生选手
int GenSpeaker(map<int, Speaker> &mapSpeaker, vector<int> &v)
{
        string str = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
        random_shuffle(str.begin(), str.end());

        for (int i = 0; i < 24; i++)
        {
                Speaker tmp;
                tmp.name = "选手";
                tmp.name = tmp.name + str[i];
                mapSpeaker.insert(pair<int, Speaker>(100+i, tmp));
        }

        for (int i = 0; i < 24; i++)
        {
                v.push_back(100+i);//参加参赛人员的名单
        }
        
        return 0;
}

//选手抽签
int speechContestDraw(vector<int> &v)
{
        random_shuffle(v.begin(), v.end());
        return 0;
}

//选手比赛
int speechContest(int index, vector<int> &v1, map<int, Speaker> &mapSpeaker, vector<int> &v2)
{

        //小组的比赛得分 记录下来 求出前三名和后三名
        multimap<int, int, greater<int>> multimapGroup; //小组成绩
        int tmpCount = 0;

        for (vector<int>::iterator it = v1.begin(); it != v1.end() ; it++)
        {      
                tmpCount++;
                //打分
                deque<int> dscore;
                for (int j = 0; j < 10;  j++) //10个评委打分
                {
                        int score = 50+rand()%50;  
                        dscore.push_back(score);
                }
                sort(dscore.begin(), dscore.end());
                dscore.pop_back(); 
                dscore.pop_front();//去除最低分 最高分
                //求平均分
                int scoresum = accumulate(dscore.begin(), dscore.end(), 0);
                int scoreavg = scoresum / dscore.size();

                mapSpeaker[*it].score[index] = scoreavg;//选手得分,存入容器中
                multimapGroup.insert(pair<int, int>(scoreavg, *it));
        }

        //处理分组
        if(tmpCount%6 == 0){
                cout<<"小组的比赛成绩"<<endl;
                for ( multimap<int, int, greater<int>>::iterator mit = multimapGroup.begin(); mit !=  multimapGroup.end(); mit++)
                {
                        //编号 姓名 得分
                        cout<< mit->second <<"  "<< mapSpeaker[mit->second].name<<"     "<< mit->first<<endl;
                }

                //前三名
                while (multimapGroup.size() > 3)
                {
                        multimap<int, int, greater<int>>::iterator it1 = multimapGroup.begin();
                        v2.push_back(it1->second); //把前三名放到v2 晋级名单中
                        multimapGroup.erase(it1);
                }

                multimapGroup.clear();//本小组 比赛成绩
        }
        return 0;
}

//查看比赛结果
void speeckContestPrint(int index, vector<int> &v, map<int, Speaker> &mapSpeaker)
{
        printf("第%d轮 晋级名单\n", index+1);
        for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
        {
                cout<<"参赛编号: "<< *it <<"\t"<<mapSpeaker[*it].name<<"\t"<<mapSpeaker[*it].score[index]<<endl;
        }
}

int main(int argc, char const *argv[])
{
        map<int, Speaker> mapSpeaker;
        vector<int> v1;//第1轮演讲比赛名单；
        vector<int> v2;//第2轮演讲比赛名单；
        vector<int> v3;//第3轮演讲比赛名单；
        vector<int> v4;//最后前三名 演讲比赛名单;

        //产生选手 得到第一轮选手的参赛名单
        GenSpeaker(mapSpeaker, v1);

        //第1轮 选手抽签 选手比赛 查看比赛结果
        cout<<"\n\n\n任意键,开始第一轮比赛"<<endl;
        cin.get();
        speechContestDraw(v1);
        speechContest(0, v1, mapSpeaker, v2);
        speeckContestPrint(0, v2,mapSpeaker);

        //第2轮 选手抽签 选手比赛 查看比赛结果
        cout<<"\n\n\n任意键,开始第二轮比赛"<<endl;
        cin.get();
        speechContestDraw(v2);
        speechContest(1, v2, mapSpeaker, v3);
        speeckContestPrint(1, v3,mapSpeaker);
        
        //第3轮 选手抽签 选手比赛 查看比赛结果
        cout<<"\n\n\n任意键,开始第三轮比赛"<<endl;
        cin.get();
        speechContestDraw(v3);
        speechContest(2, v3, mapSpeaker, v4);
        speeckContestPrint(2, v4,mapSpeaker);

        return 0;
}
